// Kony


#include "UI/Widget/BaseUserWidget.h"
#include "Components/CanvasPanelSlot.h"
#include "Blueprint/WidgetLayoutLibrary.h"
#include "Components/Overlay.h"
#include "Components/PanelWidget.h"
#include "GASC_HuanLing/BaseLogChannels.h"

// 静态变量定义
EDraggableMenuType UBaseUserWidget::ClickedMenuType = EDraggableMenuType::None;

void UBaseUserWidget::SetWidgetController(UObject* InWidgetController)
{
	//设置控件控制器时候 会执行WidgetControllerSet
	WidgetController = InWidgetController;
	WidgetControllerSet();
}

void UBaseUserWidget::NativeConstruct()
{
	Super::NativeConstruct();
	if (!bIsDraggable)
	{
		return;
	}
	if (WidgetType == EWidgetType::MainWidget)
	{
		if (CanvasPanel_Root)
		{
			if (Overlay_SpellMenu)
			{
				Overlay_SpellMenu->SetRenderTransformPivot(FVector2D(0.5f, 0.5f));
			}
			if (Overlay_AttributeMenu)
			{
				Overlay_AttributeMenu->SetRenderTransformPivot(FVector2D(0.5f, 0.5f));
			}
		}
	}
}

FReply UBaseUserWidget::NativeOnMouseWheel(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
	if (!bIsDraggable)
	{
		return Super::NativeOnMouseMove(InGeometry, InMouseEvent);
	}
	if (bIsDraggable)
	{
		if (WidgetType == EWidgetType::MainWidget)
		{
			// 主界面缩放当前菜单（若无记录类型，则缩放法术菜单作为默认）
			if (CanvasPanel_Root)
			{
				//这个是 滚轮缩放功能，只有地图用得到，这里留着以后做地图用
				// float ScrollDelta = InMouseEvent.GetWheelDelta();
				// CurrentZoom = FMath::Clamp(CurrentZoom + ScrollDelta * 0.1f, 0.5f, 3.0f);
				// UOverlay* TargetOverlay = GetOverlayByMenuType(ClickedMenuType);
				// if (!TargetOverlay)
				// {
				// 	TargetOverlay = Overlay_SpellMenu;
				// }
				// if (TargetOverlay)
				// {
				// 	TargetOverlay->SetRenderScale(FVector2D(CurrentZoom, CurrentZoom));
				// }
			}
		}
		return FReply::Unhandled();
	}
	return FReply::Unhandled();
	
}

FReply UBaseUserWidget::NativeOnMouseMove(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
	//不是可拖拽组件直接返回
	if (!bIsDraggable)
	{
		return Super::NativeOnMouseMove(InGeometry, InMouseEvent);
	}
	if (bDragging)
	{
		if (WidgetType == EWidgetType::MainWidget)
		{
			if (CanvasPanel_Root)
			{
				FVector2D CurrentScreenPos = InMouseEvent.GetScreenSpacePosition();

				// 把屏幕 delta 转换为地图父级坐标空间 delta
				FVector2D LocalDelta = InGeometry.AbsoluteToLocal(CurrentScreenPos) - InGeometry.AbsoluteToLocal(LastMouseScreenPos);

				// 获取目标 Overlay
				UOverlay* TargetOverlay = GetOverlayByMenuType(ClickedMenuType);
				if (!TargetOverlay)
				{
					TargetOverlay = Overlay_SpellMenu; // 默认
				}
				if (TargetOverlay)
				{
					FVector2D CurrentTranslation = TargetOverlay->GetRenderTransform().Translation;
					TargetOverlay->SetRenderTranslation(CurrentTranslation + LocalDelta);
				}

				LastMouseScreenPos = CurrentScreenPos;

				return FReply::Handled();
			}
		}
		return Super::NativeOnMouseMove(InGeometry, InMouseEvent);
	}
	return FReply::Unhandled();
	
}

FReply UBaseUserWidget::NativeOnMouseButtonDoubleClick(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
	//判断是否禁止点击移动
	if (bIsStopMove)
	{
		Super::NativeOnMouseButtonDoubleClick(InGeometry, InMouseEvent);
		return FReply::Handled();
	}
	else
	{
		return Super::NativeOnMouseButtonDoubleClick(InGeometry, InMouseEvent);
	}
	
}

FReply UBaseUserWidget::NativeOnMouseButtonDown(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
	//判断是否禁止点击移动
	if (bIsStopMove)
	{
		//判断是否可拖动，这里影响技能释放 所以必须包裹不能直接返回
		if (bIsDraggable)
		{
			if (WidgetType == EWidgetType::MainWidget)
			{
				if (CanvasPanel_Root)
				{
					if (InMouseEvent.GetEffectingButton() == EKeys::LeftMouseButton)
					{
						bDragging = true;
						LastMouseScreenPos = InMouseEvent.GetScreenSpacePosition();
						UE_LOG(LogHuanYue, Warning, TEXT("NativeOnMouseButtonDown 事件 CanvasPanel_Root 存在 属于主界面"));
						return FReply::Handled();
					}
				}
			}
			// 子界面：记录它代表的菜单类型，并把事件让给主界面处理
			if (WidgetType == EWidgetType::SubWidget)
			{
				ClickedMenuType = ThisWidgetMenuType; // 由蓝图设置
				Super::NativeOnMouseButtonDown(InGeometry, InMouseEvent);
				UE_LOG(LogHuanYue, Warning, TEXT("子界面"));
				//这里是用于如果是子界面 取消处理 让主界面进行拖拽操作
				return FReply::Unhandled();
			}
			UE_LOG(LogHuanYue, Warning, TEXT("没执行任何东西"));
			//这里是用于如果是子界面 取消处理 让主界面进行拖拽操作
			return FReply::Unhandled();
		}
		else{
			UE_LOG(LogHuanYue, Warning, TEXT("NativeOnMouseButtonDown 事件 无拖动勾选"));
			//这里是如果非主界面并且 是禁止移动的 那么点击UI就消费掉处理 避免移动
			Super::NativeOnMouseButtonDown(InGeometry, InMouseEvent);
			return FReply::Handled();
		}
	}
	else
	{
		return Super::NativeOnMouseButtonDown(InGeometry, InMouseEvent);
	}
}

FReply UBaseUserWidget::NativeOnMouseButtonUp(const FGeometry& InGeometry, const FPointerEvent& InMouseEvent)
{
	if (!bIsDraggable)
	{
		return Super::NativeOnMouseMove(InGeometry, InMouseEvent);
	}
	if (WidgetType == EWidgetType::MainWidget)
	{
		if (CanvasPanel_Root)
		{
			if (InMouseEvent.GetEffectingButton() == EKeys::LeftMouseButton)
			{
				bDragging = false;
				// 松开时重置，避免残留
				ClickedMenuType = EDraggableMenuType::None;
				return FReply::Handled();
			}
		}
	}
	return FReply::Unhandled();
}


void UBaseUserWidget::NativeOnMouseLeave(const FPointerEvent& InMouseEvent)
{
	// 调用父类实现
	Super::NativeOnMouseLeave(InMouseEvent);
}

UOverlay* UBaseUserWidget::GetOverlayByMenuType(EDraggableMenuType MenuType) const
{
	switch (MenuType)
	{
	case EDraggableMenuType::SpellMenu:
		return Overlay_SpellMenu;
	case EDraggableMenuType::AttributeMenu:
		return Overlay_AttributeMenu;
	case EDraggableMenuType::None:
	default:
		return nullptr;
	}
}
